package net.htjs.mobile.player;

import net.htjs.mobile.Chess;
import net.htjs.mobile.ChessBoard;
import net.htjs.mobile.Constants;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

public class Nami extends BaseAI{
	
	public HandlerThread handler;
	CalWeightHandler calWeightHandler ; 
	private static int weight=0;
	public Nami(boolean myFlag) {
		super(myFlag);
		Log.i("构造函数", "nami player");
		handler = new HandlerThread("calculate_handler");
		handler.start();
		calWeightHandler = new CalWeightHandler(handler.getLooper());
	}


	public boolean isMyChess(int x, int y) {
		
		return isMyChess(new Chess(x,y));
	}


	@Override
	public Chess run() {
		calWeightHandler.sendEmptyMessage(0);
		return BaseAI.bestPoint;
	}

	 class CalWeightHandler extends Handler{
		private int playerFlag;
		private int enemyFlag;
		private boolean halfLife = false;
	    private boolean deadPoint = false;
	    private Chess myChess;
	    private Chess tmpChess;
	    private int chessCount;
	    private int orientation;
	    
		public CalWeightHandler() {
			super();
		}
		

		public CalWeightHandler(Looper looper) {
			super(looper);
		}


		public int howWeight(int x, int y, boolean whoschess) {
			//BaseAI.bestPoint=new Chess(0,0);
			playerFlag = whoschess?ChessBoard.BLACK:ChessBoard.WHITE;
            enemyFlag = whoschess?ChessBoard.WHITE:ChessBoard.BLACK;
            myChess= new Chess(x, y);
            myChess.setFlag(playerFlag);
            tmpChess=myChess;
            //initial
            weight = 0;
            weight+=cal(Constants.HORIZONTAL);
            weight+=cal(Constants.VERTICAL);
            weight+=cal(Constants.SLANT);
            weight+=cal(Constants.BACKWARD_SLANT);
            
			return weight;
		}
		
		public int cal(char direction){
			init();
			while (true) {
            	if(tmpChess.isSameType(tmpChess.nextChess(direction,orientation))){//这个方向的下个棋子是否是自己的
            		chessCount++;
            		tmpChess=tmpChess.nextChess(direction,orientation);
            	}else if (!tmpChess.isEmpty()) { //下个棋不是自己的就放过来再数
					if(orientation==Constants.BEHIND&&halfLife==true)
						deadPoint=true;//反方向数且已经是半条命就肯定是死棋了
					halfLife=true;//下个棋不是自己的就为半条命了
            		if(orientation==Constants.FRONT){//反向
            			orientation=Constants.BEHIND;
            			tmpChess=myChess;
            		}else break;
				}else {//下个位置没棋
					if(orientation==Constants.FRONT){
            			orientation=Constants.BEHIND;
            			tmpChess=myChess;
            		}else break;
				}
			}
			return calCount(chessCount);
		}
		public void init(){
			chessCount=1;
			halfLife = false;
            deadPoint = false;
            orientation=Constants.FRONT;
		}
		/**
		 * 计算棋子数所占的权重
		 * @param chessCount 己方棋子落下后那行棋子数
		 * @param halfLife 如果一行有一边被对方棋子堵住了 为 true
		 * @param deadPoint 两边都为对方棋子时为true
		 * @return
		 */
		public int calCount(int chessCount){
			int multiple=0;
			int rectify=0;
			switch (chessCount) {
			case 5:
				return 100000;
			case 4:
				if(halfLife){
					multiple=5461;
					rectify=1;
				}else{
					multiple=100000;
					rectify=-1;
				}
				break;
			case 3:
				if(halfLife){
					multiple=341;
					rectify=0;
				}else{
					multiple=1365;
					rectify=0;
				}
				break;
			case 2:
				if(halfLife){
					multiple=21;
					rectify=0;
				}else{
					multiple=85;
					rectify=0;
				}
				break;
			case 1:
				if(halfLife){
					multiple=1;
					rectify=1;
				}else{
					multiple=5;
					rectify=0;
				}
				break;
			}
			if(deadPoint){
				return -1365;
			}
			return multiple;
		}
		@Override
		public void handleMessage(Message msg) {
				if (ChessBoard.chessArray.length>0) {
					Log.i("nami", "thread name:"+Thread.currentThread().getName()+"thread id:"+Thread.currentThread().getId());
					BaseAI.bestPointList.clear();
				     for (int i = 0; i < ChessBoard.maxX; i++) {
						for (int j = 0; j < ChessBoard.maxY; j++) {
							BaseAI.weights[i][j] = 0;
						}
					}
					for (int i = CalculateRange.xstart; i <= CalculateRange.xstop; i++) {
						for (int j = CalculateRange.ystart; j <= CalculateRange.ystop; j++) {
							if (ChessBoard.chessArray[i][j] == ChessBoard.EMPTY) {
								
							BaseAI.weights[i][j]= howWeight(i, j,ChessBoard.PLAYER2FLAG);
							BaseAI.weights[i][j]+= howWeight(i, j,ChessBoard.PLAYER1FLAG);
							if(BaseAI.weights[i][j]>BaseAI.weights[BaseAI.bestPoint.getX()][BaseAI.bestPoint.getY()]){
								BaseAI.bestPoint.setX(i);
								BaseAI.bestPoint.setY(j);
								BaseAI.bestPointList.clear();
								BaseAI.bestPointList.add(BaseAI.bestPoint);
								Log.i("BaseAI.bestPoint", BaseAI.bestPoint.toString());
							}else if (BaseAI.weights[i][j]==BaseAI.weights[BaseAI.bestPoint.getX()][BaseAI.bestPoint.getY()]) {
								BaseAI.bestPointList.add(new Chess(i, j));
							}
							}
						}
					}
					BaseAI.bestPoint=BaseAI.bestPointList.size()>0 ? BaseAI.bestPointList.get(random.nextInt(BaseAI.bestPointList.size())):null;
					ChessBoard.computerRun.sendEmptyMessage(0);
				}
		}
	}
}
